<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>递归函数 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter02.html" />
    
    
    <link rel="prev" href="./index.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="1"
        data-chapter-title="递归函数"
        data-filepath="chapter01.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 13:35:09 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        递归函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        匿名函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高阶函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        闭包的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        装饰器的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x9012;&#x5F52;&#x51FD;&#x6570;">&#x9012;&#x5F52;&#x51FD;&#x6570;</h2>
<h3 id="&#x4EC0;&#x4E48;&#x662F;&#x9012;&#x5F52;&#x51FD;&#x6570;"><1>&#x4EC0;&#x4E48;&#x662F;&#x9012;&#x5F52;&#x51FD;&#x6570;</1></h3>
<p>&#x901A;&#x8FC7;&#x524D;&#x9762;&#x7684;&#x5B66;&#x4E60;&#x77E5;&#x9053;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5728;&#x5185;&#x90E8;&#x4E0D;&#x8C03;&#x7528;&#x5176;&#x5B83;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x662F;&#x81EA;&#x5DF1;&#x672C;&#x8EAB;&#x7684;&#x8BDD;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5C31;&#x662F;&#x9012;&#x5F52;&#x51FD;&#x6570;&#x3002;</p>
<h3 id="&#x9012;&#x5F52;&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;"><2>&#x9012;&#x5F52;&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;</2></h3>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x8BA1;&#x7B97;&#x9636;&#x4E58; <code>n! = 1 * 2 * 3 * ... * n</code></p>
<p>&#x89E3;&#x51B3;&#x529E;&#x6CD5;1:&#x4F7F;&#x7528;&#x5FAA;&#x73AF;&#x6765;&#x5B8C;&#x6210;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cal</span><span class="hljs-params">(num)</span>:</span>
    result,i = <span class="hljs-number">1</span>,<span class="hljs-number">1</span>
    <span class="hljs-keyword">while</span> i &lt;= num:
        result *= i
        i+= <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> result

print(cal(<span class="hljs-number">3</span>))
</code></pre>
<h5 id="&#x770B;&#x9636;&#x4E58;&#x7684;&#x89C4;&#x5F8B;">&#x770B;&#x9636;&#x4E58;&#x7684;&#x89C4;&#x5F8B;</h5>
<pre><code>1! = 1
2! = 2 &#xD7; 1 = 2 &#xD7; 1!
3! = 3 &#xD7; 2 &#xD7; 1 = 3 &#xD7; 2!
4! = 4 &#xD7; 3 &#xD7; 2 &#xD7; 1 = 4 &#xD7; 3!
...
n! = n &#xD7; (n-1)!
</code></pre><p>&#x89E3;&#x51B3;&#x529E;&#x6CD5;2:&#x4F7F;&#x7528;&#x9012;&#x5F52;&#x6765;&#x5B9E;&#x73B0;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">factorial</span><span class="hljs-params">(num)</span>:</span>
    result = <span class="hljs-number">1</span>
    <span class="hljs-keyword">if</span> num == <span class="hljs-number">1</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    result = num * factorial(num -<span class="hljs-number">1</span>)
    <span class="hljs-keyword">return</span> result
print(cal(<span class="hljs-number">3</span>))
</code></pre>
<p>&#x539F;&#x7406;</p>
<p><img src="imgs/&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#x9636;&#x4E58;.png" alt="&#x9636;&#x4E58;&#x9012;&#x5F52;&#x539F;&#x7406;"></p>
<ul>
<li>&#x7EC3;&#x4E60;&#xFF1A;&#x4F7F;&#x7528;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x3002;1&#x3001;1&#x3001;2&#x3001;3&#x3001;5&#x3001;8&#x3001;13&#x3001;21&#x3001;34&#x3001;&#x2026;&#x2026;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./index.html" class="navigation navigation-prev " aria-label="Previous page: 学习目标"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter02.html" class="navigation navigation-next " aria-label="Next page: 匿名函数"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
